﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SPCAMLEditor.Dialogs
{
    public partial class FindNodeDialog : Form
    {
        public FindNodeDialog()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.txtNodeText.Focus();
        }

        private TreeNode _CurrentNode;
        public TreeNode CurrentNode
        {
            set
            {
                _CurrentNode = value;
                this.Text = "Find: " + value.Text;
                this.currentFindIndex = 0;
            }
            get
            {
                return _CurrentNode;
            }
        }
        int currentFindIndex = 0;

        private void btnOK_Click(object sender, EventArgs e)
        {
            string searchText = this.txtNodeText.Text.Trim();
            if (searchText == String.Empty)
            {
                MessageBox.Show(this, "Please enter a key word.");
                return;
            }

            if (currentFindIndex >= this.CurrentNode.Nodes.Count)
            {
                this.currentFindIndex = 0;
            }

            for (int i = currentFindIndex; i < this.CurrentNode.Nodes.Count; i ++)
            {
                var node = this.CurrentNode.Nodes[i];
                if (node.Text.Contains(searchText))
                {
                    this.currentFindIndex = i+1;
                    node.BackColor = Color.Yellow;
                    node.TreeView.SelectedNode = node;
                    return;
                }
            }
            currentFindIndex = 0;
            MessageBox.Show(this,"Find has meet the end.");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
